package hservice

import (
	"encoding/json"

	"advertoper/db"
	"advertoper/errorcode"
	"advertoper/rpc/client/zjservice"

	"doubimeizhi.com/datatable"
	// // "doubimeizhi.com/utility"

	log "github.com/cihub/seelog"
)

var (
	defaultServicesUser *ServicesUser
)

type ServicesUser struct {
}

func newServicesUser() *ServicesUser {
	service := &ServicesUser{}
	return service
}

func GetServicesUser() *ServicesUser {
	if defaultServicesUser == nil {
		defaultServicesUser = newServicesUser()
	}
	return defaultServicesUser
}

func (p *ServicesUser) GetServiceData(regionCode string, versionCode, plat, source int) (uint, string, []*RequestData) {
	//先从总体容器布局中获得容器的列表顺序
	containerOrder := GetUserInfoService().getContainerOrder(versionCode, plat, source)
	//遍历取到容器数据
	data := make([]*RequestData, 0)
	for _, id := range containerOrder {
		daq := datatable.NewDataTableQuery()
		daq.SetTableName("container")
		daq.PushField(&datatable.FiledItem{Alias: "container_type", Field: "container_type"})
		daq.PushField(&datatable.FiledItem{Alias: "container_name", Field: "container_name"})
		daq.PushField(&datatable.FiledItem{Alias: "more_routers", Field: "more_routers"})
		daq.PushField(&datatable.FiledItem{Alias: "layout", Field: "layout"})
		daq.PushField(&datatable.FiledItem{Alias: "item_order", Field: "item_order"})
		daq.PushCondition("container_id = ?", id)
		sql, values := daq.GenerateSQL()
		mysqlHelper := db.GetHomePageInfoMysqlHelper()
		results, err := mysqlHelper.Query(sql, values...)
		if err != nil {
			log.Errorf("query container data fail ,container_id is %d err is %s", id, err.Error())
			continue
		}
		if len(results) == 0 {
			continue
		}
		item := &RequestData{}
		item.ContainerId = id
		item.ContainerType = mysqlHelper.GetColInt(results[0], "container_type")
		item.ContainerName = mysqlHelper.GetColString(results[0], "container_name")
		item.MoreRouters = mysqlHelper.GetColString(results[0], "more_routers")

		layout := make(map[string]interface{})
		ly := results[0]["layout"].([]byte)
		json.Unmarshal(ly, &layout)
		item.Layout = layout

		itemOrder := make([]int, 0)
		io := results[0]["item_order"].([]byte)
		json.Unmarshal(io, &itemOrder)
		//从缓存中取到元素[]id对应数据
		dataItem := make([]map[string]interface{}, 0)
		switch item.ContainerType {
		case 1:
			list := GetZJBannerInfoService().UserGetBannerList(itemOrder, regionCode)
			if len(list) == 0 { //元素为空，则该容器不显示
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		case 2:
			list := GetZJNewsInfoService().UserGetZJNewsList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		case 3:
			//从服务中心拉取数据
			serviceItem := zjservice.GetServiceItem(source)
			if serviceItem != nil {
				if serviceItem.Code == 0 {
					if serviceItem.Data == nil {
						serviceItem.Data = make([]map[string]interface{}, 0)
					}
					item.Item = serviceItem.Data
				}
			}

		case 4:
			list := GetZJSingleViewInfoService().UserGetZJSingleView(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		case 5:
			list := GetZJGoodsListInfoService().UserGetZJGoodsListList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			for i := 0; i < len(list); i++ {
				dataItem[i]["price"] = list[i].Price
				dataItem[i]["sell_number"] = list[i].SellNumber
			}
			item.Item = dataItem
		case 7:
			list := GetZJSeckillInfoService().UserGetZJSeckillList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			for i := 0; i < len(list); i++ {
				dataItem[i]["seckill_start"] = list[i].SeckillStart
				dataItem[i]["seckill_end"] = list[i].SeckillEnd
				dataItem[i]["price"] = list[i].Price
				dataItem[i]["sell_number"] = list[i].SellNumber
				dataItem[i]["total_number"] = list[i].TotalNumber
			}

			item.Item = dataItem
		case 6:
			list := GetZJTopicInfoService().UserGetZJTopicList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		case 8:
			list := GetZJPictureInfoService().UserGetPictureList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		case 9:
			list := GetZJPageViewInfoService().UserGetPageViewList(itemOrder, regionCode)
			if len(list) == 0 {
				continue
			}
			b, _ := json.Marshal(list)
			json.Unmarshal(b, &dataItem)
			item.Item = dataItem
		}
		data = append(data, item)

	}
	return errorcode.EC_GL_SUCCESS, errorcode.Msg(errorcode.EC_GL_SUCCESS), data
}
